package com.jhws.business.repair.controller;

import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.business.property.service.PropertyService;
import com.jhws.business.repair.bo.Repair;
import com.jhws.business.repair.bo.RepairConstant;
import com.jhws.business.repair.bo.RepairUpdateData;
import com.jhws.business.repair.service.RepairService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.core.dataUpdate.service.DataUpdateService;
import com.jhws.util.DateUtil;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("Duplicates")
@Controller
@RequestMapping("/comm/repair")
public class RepairController {
    @Resource
    private RepairService repairService;
    @Resource
    private ITblOwnerDimService ownerDimService;
    @Resource
    private DataUpdateService updateService;
    @Resource
    private PropertyService propertyService;

    /**
     * 保存处理结果
     */
    @RequestMapping("saveTreatmentResult.x")
    @ResponseBody
    public JsonRespData saveTreatmentResult(Repair repair) throws Exception {
        String set;
        String where = " where id = '" + repair.getId() + "'";
        if (repair.getState() == RepairConstant.REPAIR_STATE_ACCEPTED) {
            set = "set state = '" + repair.getState() +
                    "', repair_per='" + repair.getRepairPer() +
                    "', repair_per_phone='" + repair.getRepairPerPhone() + "'";
        } else {
            set = "set treatment_result = '" + repair.getTreatmentResult() +
                    "', state = '" + repair.getState() + "'";
        }


        Date now = new Date();
        set += ",last_modify_time = '" + DateUtil.parseDateFormat(now) + "',timestamp = '" + now.getTime() + "'";
        repairService.updateRepair(set, where);

        TblOwnerDimBo owner = ownerDimService.findTblOwnerDimById(repair.getOwnerCode());
        if (owner != null) {
            String title = "[" + owner.getCommName() + "] 您的报修有进度更新";
            updateService.pushAndUpdate(now.getTime(), RepairUpdateData.REPAIR, owner.getOwnerMob(), true, true, title, "");
        }
        return new JsonRespData("", 1, "成功");
    }

    /**
     * 查询反馈信息
     */
    @RequestMapping("query.x")
    @ResponseBody
    public Object queryRepair(@RequestParam Map param) {
        return repairService.findRepairListPage(param);
    }

    /**
     * 查询业主信息
     */
    @RequestMapping("queryOwnerInfo.x")
    @ResponseBody
    public JsonRespData queryOwnerInfo(String myOwnerCode, String ownerCode) {
        TblOwnerDimBo ownerDimBo = ownerDimService.findTblOwnerDimById(ownerCode);
        Map map = null;
        if (StringUtils.isNotEmpty(myOwnerCode)) {
            map = propertyService.getStaffInfoByOwnerCode(myOwnerCode);
        }
        if (map == null){
            map = new HashMap();
        }
        map.put("ownerInfo", ownerDimBo);
        return new JsonRespData("", 1, map);
    }

    /**
     * 删除报修
     */
    @RequestMapping("deleteRepair.x")
    @ResponseBody
    public JsonRespData deleteRepair(Repair repair) {
        repairService.deleteRepair(repair);
        return new JsonRespData("", 1, "");
    }
}
